/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TrangSuc;



import Func.Conn;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author URUKU
 */
@WebServlet(name = "getTS", urlPatterns = {"/getTS"})
public class getTS extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
           String loai1 = request.getParameter("q");
           
    String loai2 = request.getParameter("ts");
    Connection con = Conn.GetCon();
    Statement statement = con.createStatement();
    Statement statement1 = con.createStatement();
 ResultSet resultSet1 ;
  DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
        decimalFormatSymbols.setDecimalSeparator('.');
        decimalFormatSymbols.setGroupingSeparator(',');
        DecimalFormat decimalFormat = new DecimalFormat("#,##0", decimalFormatSymbols);

// Execute a statement
 ResultSet resultSet = statement.executeQuery
 ("select * from thongtinsanpham,thongtinchungloai"
         + " where thongtinsanpham.machungloai = thongtinchungloai.machungloai"
         + " and thongtinchungloai.machungloai = '"+ loai1 +"' "
         + "and thongtinsanpham.maloaitrangsuc = '"+ loai2 +"'");
out.println("<table class='show' width='97%' border='0' align='center' >");
out.println("<tr align = 'center'>");
int x = 0;
String url = "";
// Iterate through the result and print the student names
while (resultSet.next())
    
      {
x+=1;    
    out.println("<td class = 'show'>");
    if(  resultSet.getInt("makhuyenmai") == 0)
               {
        url =  resultSet.getString("urlhinh") + "1s.jpg";
out.println("<a href ='chitietsp.jsp?id="+resultSet.getString("MaSP")+"'><img src ="+url+" height ='225px' weight ='225px'></a><br>" );
out.println(""+ resultSet.getString("TenChungLoai")+"<br>");
out.println("Giá :"+ decimalFormat.format(resultSet.getInt("Gia"))+" VND <br>");
out.println("Số lượng có hạn");
    }
 else
         {
        
        url =  resultSet.getString("urlhinh") + "1-" + resultSet.getInt("makhuyenmai") + ".jpg";
out.println("<a href ='chitietsp.jsp?id="+resultSet.getString("MaSP")+"'><img src ="+ url +"></a><br>" );
out.println(""+ resultSet.getString("TenChungLoai")+"<br>");
out.println("Giá khuyến mãi :"+ decimalFormat.format((resultSet.getDouble("Gia") - (resultSet.getDouble("Gia")* resultSet.getDouble("MaKhuyenMai"))/10)) +" VND  <br>");
out.println("Giá cũ :<b class ='gia'>"+ decimalFormat.format(resultSet.getInt("Gia")) +"</b> VND  <br>");
 
 }

out.println("<table border='0' width ='97%' >");
out.println  ("<tr><td >");
out.println("<center><form action = 'Xuly?act=cart&id="+ resultSet.getString("MaSP") +"&sl=1' method = 'post'>");
out.println("<input class='btmua' type=\"submit\" value=\"Mua Hàng\">");

out.println("</form></center>");
out.println("</td>");
out.println("</tr>");
out.println("</table>");

out.println("</td>");
    if(x ==3)
		 {
			 x = 0;
            out.println("</tr>"); 
	out.println("<tr align='center'>");		 
        }
}       
out.println("</tr></table>");
               
        }
        catch (SQLException e){
        e.printStackTrace();
        }
                finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
